﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using TrafficCountingTool.Core;

namespace TrafficCountingTool.Models
{
    public class DisplayingVehicleCounting : INotifyPropertyChanged
    {
        private string _vehicleType;
        private int _count;
        private ICommand _increaseCommand;
        private ICommand _decreaseCommand;
        private ICommand _resetCommand;

        public string VehicleType
        {
            get { return _vehicleType; }
            set
            {
                _vehicleType = value;
                OnPropertyChanged("VehicleCountingType");
            }
        }

        public int Count
        {
            get { return _count; }
            set
            {
                _count = value;
                OnPropertyChanged("Count");
            }
        }

        public ICommand IncreaseCommand
        {
            get { return _increaseCommand ?? (_increaseCommand = new DelegateCommand(o => Count++)); }
        }

        public ICommand DecreaseCommand
        {
            get
            {
                return _decreaseCommand ?? (_decreaseCommand = new DelegateCommand(o =>
                {
                    if (Count > 0)
                    {
                        Count--;
                    }
                }));
            }
        }

        public ICommand ResetCommand
        {
            get
            {
                return _resetCommand ?? (_resetCommand = new DelegateCommand(o =>
                {
                    if (Count == 0)
                        return;

                    if (MessageBox.Show(string.Format("是否复位{0}计数", VehicleType), "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        Count = 0;
                    }
                }));
            }
        }

        public List<TimeSpan> CreateTime { get; set; }

        public DisplayingVehicleCounting()
        {
            CreateTime = new List<TimeSpan>();
        }

        public VehicleCounting ToVehicleCounting()
        {
            return new VehicleCounting()
            {
                Count = Count,
                VehicleType = VehicleType,
                CreateTime = CreateTime
            };
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}